package com.example.mall.common.utils;

import com.example.mall.common.enums.ModuleEnum;
import com.example.mall.common.enums.SymbolEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;

import java.net.URL;
import java.util.Arrays;
import java.util.List;

/**
 * 资源工具类
 */
public class ResourceHelper {

    public static final String ROOT_PACKAGE_NAME = "/com/example/mall";

    public static String getResourceAsString(Class<?> cls, String resource) {
        Assert.notNull(cls, "cls不能为空");
        Assert.notNull(resource, "resource不能为空");
        URL rootResource = cls.getClassLoader().getResource(StringUtils.EMPTY);
        String moduleName = ModuleEnum.getModuleCodeIfContains(cls.getName());
        List<String> resources = Arrays.asList(String.valueOf(rootResource), ROOT_PACKAGE_NAME, moduleName);
        return SymbolEnum.concatWithSlash(resources).concat(resource);
    }

}
